package com.luo.ai.mcp.server.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.luo.ai.mcp.server.resources.MyBaseResource;
import io.modelcontextprotocol.server.McpServerFeatures;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 资源配置
 *
 * @author luohq
 * @date 2025-04-27 13:14
 */
@Configuration
public class ResourcesConfig {

    ///**
    // * 注册资源
    // *
    // * @return 资源列表
    // */
    //@Bean
    //public List<McpServerFeatures.SyncResourceSpecification> myResources() {
    //    //资源描述
    //    McpSchema.Resource systemInfoResource = new McpSchema.Resource(
    //            "system/info",
    //            "System Information",
    //            "Provides system information",
    //            MediaType.APPLICATION_JSON_VALUE,
    //            //可选
    //            new McpSchema.Annotations(
    //                    //适用角色
    //                    List.of(McpSchema.Role.ASSISTANT),
    //                    //重要性（1最重要, 0最不重要）
    //                    0D
    //            )
    //    );
    //    //资源说明
    //    McpServerFeatures.SyncResourceSpecification resourceSpecification = new McpServerFeatures.SyncResourceSpecification(systemInfoResource, (exchange, request) -> {
    //        try {
    //            Map systemInfo = Map.of(
    //                    "info", "我是MCP Server"
    //            );
    //            String jsonContent = new ObjectMapper().writeValueAsString(systemInfo);
    //            return new McpSchema.ReadResourceResult(List.of(
    //                    new McpSchema.TextResourceContents(request.uri(), "application/json", jsonContent)
    //            ));
    //        } catch (Exception e) {
    //            throw new RuntimeException("Failed to generate system info", e);
    //        }
    //    });
    //
    //    return List.of(resourceSpecification);
    //}

    /**
     * 注册同步资源
     *
     * @return 资源列表
     */
    @Bean
    public List<McpServerFeatures.SyncResourceSpecification> mySyncResources(List<MyBaseResource> myBaseResources) {
        return myBaseResources.stream()
                .filter(myBaseResource -> !myBaseResource.isAsync())
                .map(myBaseResource -> myBaseResource.buildSyncResource())
                .collect(Collectors.toList());
    }

    /**
     * 注册异步资源
     *
     * @return 资源列表
     */
    @Bean
    public List<McpServerFeatures.AsyncResourceSpecification> myAsyncResources(List<MyBaseResource> myBaseResources) {
        return myBaseResources.stream()
                .filter(myBaseResource -> myBaseResource.isAsync())
                .map(myBaseResource -> myBaseResource.buildAsyncResource())
                .collect(Collectors.toList());
    }


}
